package com.myservlet01;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Servlet03
 */
public class Servlet01 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Servlet01() {
        super();
        // TODO Auto-generated constructor stub
    }
    ServletConfig config_;
    @Override
    public void init(ServletConfig config) throws ServletException {
    	// TODO Auto-generated method stub
    	super.init(config);
    	
    	this.config_= config;
	    System.out.println("init()");
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		
		  // 13、可以获取servlet配置信息（一）
	    String encoding= config_.getInitParameter("encoding");
	    System.out.println("init-param:"+ encoding);
	    
	    

            ServletContext application=this.getServletContext();
		    application.setAttribute("name", "tony");
		  
	//    19、实现Servlet的转发
	 //  application.getRequestDispatcher("/Servlet02").forward(request, response);
		    
		    
		    
	//	response.getWriter().append("Served at: ").append(request.getContextPath());
			HttpSession session= request.getSession();
			String name=(String) session.getAttribute("username");
			if(name!=null) {
				System.out.println(name);
			}
			System.out.println(session.getId());
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	 
	/***
	 * 应用被卸载的时候，选中应用，删除
	 */
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
		
		  System.out.println("destroy()");
	}


}
